<template>
  <view class="course-list">
    <view class="course-list__header">
      <text class="course-list__title">课程列表</text>
      <view v-if="loading" class="course-list__loading">加载中...</view>
    </view>
    
    <view v-if="error" class="course-list__error">
      {{ error }}
    </view>
    
    <view v-else class="course-list__content">
      <view 
        v-for="(course, index) in courses" 
        :key="index" 
        class="course-item"
      >
        <text class="course-item__name">{{ course }}</text>
      </view>
      
      <view v-if="courses.length === 0 && !loading" class="course-list__empty">
        暂无课程数据
      </view>
    </view>
  </view>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue';
import { ApiService } from '../service/api';

export default defineComponent({
  name: 'CourseList',
  setup() {
    // 状态管理
    const courses = ref<string[]>([]);
    const loading = ref<boolean>(false);
    const error = ref<string>('');

    // 获取课程列表
    const fetchCourses = async () => {
      loading.value = true;
      error.value = '';
      
      try {
        courses.value = await ApiService.getCourseList();
      } catch (err: any) {
        error.value = err.msg || '获取课程列表失败';
        console.error('获取课程列表失败:', err);
      } finally {
        loading.value = false;
      }
    };

    // 组件挂载时加载数据
    onMounted(() => {
      fetchCourses();
    });

    return {
      courses,
      loading,
      error,
      fetchCourses
    };
  }
});
</script>

<style lang="scss">
.course-list {
  padding: 20rpx;
  
  &__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;
  }
  
  &__title {
    font-size: 32rpx;
    font-weight: bold;
  }
  
  &__loading {
    font-size: 24rpx;
    color: #999;
  }
  
  &__error {
    padding: 30rpx;
    text-align: center;
    color: #ff4d4f;
    background-color: #fff2f0;
    border-radius: 8rpx;
  }
  
  &__empty {
    padding: 60rpx 0;
    text-align: center;
    color: #999;
    font-size: 28rpx;
  }
}

.course-item {
  padding: 20rpx;
  margin-bottom: 16rpx;
  background-color: #ffffff;
  border-radius: 8rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  
  &__name {
    font-size: 28rpx;
    color: #333;
  }
}
</style> 